package com.hsh.gaoqianlianmeng.domain;

import com.hsh.gaoqianlianmeng.anno.Alia;
import com.hsh.gaoqianlianmeng.anno.SheetInfo;
import com.hsh.gaoqianlianmeng.constants.RegexConstants;
import lombok.Data;
import lombok.experimental.Accessors;
import org.apache.logging.log4j.util.Strings;

import java.math.BigDecimal;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Data
@SheetInfo(headerRowIndex = 1)
@Accessors(chain = true)
public class YangImport {

    @Alia("销售订单号")
    private String no;

    @Alia("洋包裹运单号")
    private String yangExpressNo;

    @Alia("收件人")
    private String customerName;

    @Alia("身份证号")
    private String customerIdNo;

    @Alia("手机")
    private String customerPhone;

    @Alia("地址")
    private String customerAddress;

    @Alia("包裹毛重")
    private BigDecimal expressWeight;

    @Alia("物品名称")
    private String productName;
    /**
     * 商品编码
     */
    @Alia("商品条形码")
    private String productCode;

    @Alia("邮税则号")
    private String yzsh;

    @Alia("原产地国别代码")
    private String ycgcode;

    /**
     * 商品编码对应的单价
     */

    @Alia("单价")
    private BigDecimal unitPrice;

    @Alia("数量")
    private Integer num;

    @Alia("净重")
    private String jingzhong;

    @Alia("品牌")
    private String pinpai;

    /**
     * 必填 Fulfilled By iParcel（FBi），使用洋包裹仓储打包服务的客户填大写的Y，反之填写N
     */
    @Alia("FBI")
    private String FBI;
    /**
     * 必填 需要快递公司香港单号填Y，反之填N
     */
    @Alia("国际单号")
    private String trackingNumber;

    @Alia("订单备注")
    private String note;

    @Alia("备注2")
    private String note2;

    @Alia("包装要求")
    private String packagingRequirements;

    @Alia("备注3")
    private String note3;

    /**
     * 按照 商品编码前缀 *-X 出现的次数 排序
     * 1~出现次数最多
     */
    private Integer codeIndex;

    /**
     * 商品编码为空，设置为"---"
     *
     * @return
     */
    public YangImport init() {
        this.productCode = Strings.isBlank(this.productCode) ? "---" : this.productCode;
        this.FBI = "N";
        this.trackingNumber = "N";
        // 将地址后面的虚拟号码拼接到手机号后面 地址[1234]-> 手机号-1234
        Matcher matcher = Pattern.compile(RegexConstants.VIRTUAL_NUM_SUF).matcher(this.customerAddress);
        if (matcher.find()) {
            String virtualNumSuffer = matcher.group().replace("[", "-").replace("]", "");
            this.customerPhone = this.customerPhone + virtualNumSuffer;
        }
        return this;
    }

    /**
     * 如果0.商品信息中有商品名称，单价
     * 则使用这些信息
     *
     * @param product
     */
    public YangImport initByProduct(Product product) {
        if (product == null) {
            return this;
        }

        // 如果0.商品信息中有商品名称，用这个商品名称
        String productName = product.getProductName();
        if (Strings.isNotBlank(productName)) {
            this.productName = productName;
        }

        // 如果0.商品信息中有单价，用这个单价
        BigDecimal unitPrice = product.getUnitPrice();
        if (unitPrice != null && unitPrice.compareTo(BigDecimal.ZERO) > 0) {
            this.unitPrice = unitPrice;
        }
        return this;
    }

    /**
     * CC订单 国际单号设置为N
     *
     * @return
     */
    public YangImport CCInit() {
        this.init();
        return this;
    }

    /**
     * HNCC订单 国际单号设置为N
     *
     * @return
     */
    public YangImport HNCCInit() {
        this.init();
        return this;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        YangImport that = (YangImport) o;
        return Objects.equals(no, that.no);
    }

    @Override
    public int hashCode() {
        return Objects.hash(no);
    }

}
